<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-9
 * Time: 16:54
 */


namespace app\manage\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\common\model\Position as PositionModel;
use app\common\model\Banner as BannerModel;
use think\Db;

class Position extends Admin
{
    /**
     * 轮播图列表
     * @return mixed
     */
    public function lists($position_id = null)
    {
        if ($position_id === null) $this->error('缺少参数');
        //获取查询条件
        $map = $this->getMap();
        // 数据列表
        $data_list = BannerModel::where($map)
            ->where('position_id', $position_id)
            ->order('create_time desc')
            ->paginate();
        // 使用ZBuilder快速创建数据表格
        $btn_access1 = [
            'title' => '修改轮播图',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href' => url('edit', ['id' => '__id__', 'position_id' => $position_id]),
        ];
        $btn_access = [
            'title' => '新增轮播图',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add', ['position_id' => $position_id]),
        ];
        $btn_access2 = [
            'title' => '删除轮播图',
            'icon' => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default',
            'href' => url('delete', ['id' => '__id__', 'position_id' => $position_id]),
        ];
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setPageTitle('轮播图管理')// 设置页面标题
            ->addColumns([ // 批量添加列
                ['id', 'ID'],
                ['imgurl', '轮播图', 'picture'],
                ['status', '状态', 'status', '', ['显示:success', '不显示:danger']],
                ['right_button', '操作', 'btn']
            ])
            ->setPrimaryKey('id')
            ->addTopButton('add', $btn_access)// 添加顶部按钮
            ->addRightButton('edit', $btn_access1)// 添加右侧按钮
            ->addRightButton('del', $btn_access2)// 添加右侧按钮
            ->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染页面
    }

    /**
     * 新增
     * @return mixed
     * @author
     */
    public function add($position_id = null)
    {

        // 保存数据
        if ($this->request->isPost()) {
            $info = PositionModel::get($position_id);
            if ($info['is_flash'] == '2') {
                $banner = Db::name('qt_banner')->where('position_id', $position_id)->count();
                if ($banner >= 1) {
                    $this->error('不轮播只能上传一张广告');
                }
            }
            // 表单数据
            $data = $this->request->post();
            $data['position_id'] = $position_id;
            if (empty($data['imgurl'])) {
                $this->error('请上传图片');
            }
            if (empty($data['position_id'])) {
                $this->error('请选择广告位置');
            }
            if ($advert = BannerModel::create($data)) {
                // 记录行为
                $this->success('新增成功', 'lists?position_id=' . $position_id);
            } else {
                $this->error('新增失败');
            }
        }
        if ($position_id === null) $this->error('缺少参数');
        $store = \app\common\model\Notice::column('title', 'id');
        $title = '新增广告';
        // 显示添加页面
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'position_id', $position_id],
                ['image', 'imgurl', '轮播图', '<code>必填</code>'],
                ['select', 'active_id', '选择关联公告', '', $store],
                ['radio', 'status', '状态', '', ['显示', '不显示'], 0]
            ])
            ->setPageTitle($title)
            ->fetch();
    }

    /**
     * 编辑
     * @param null $id 用户id
     * @return mixed
     * @author
     */
    public function edit($id = null, $position_id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            if (empty($data['imgurl'])) {
                $this->error('请上传图片');
            }
            if (BannerModel::update($data)) {
                $this->success('编辑成功', 'lists?position_id=' . $position_id);
            } else {
                $this->error('编辑失败');
            }
        }
        $info = BannerModel::get($id);
        $position_id = $info['position_id'];
        $store = \app\common\model\Notice::column('title', 'id');
        // 显示编辑页面
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['hidden', 'position_id', $position_id],
                ['image', 'imgurl', '轮播图', '<code>必填</code>'],
                ['select', 'active_id', '选择关联公告', '', $store],
                ['radio', 'status', '状态', '', ['显示', '不显示'], 0]
            ])
            ->setFormData($info)
            ->fetch();
    }

    //删除轮播图
    public function delete($id = null, $position_id = null)
    {
        if ($id === null) $this->error('缺少参数');
        BannerModel::where('id', $id)->delete();
        $this->success('删除成功', 'banner?position_id=' . $position_id);
    }
}